from django.utils.datetime_safe import datetime
from rest_framework import serializers
from goods.models import SKU
from .models import OrderInfo, OrderGoods
from decimal import Decimal
from django_redis import get_redis_connection
from django.db import transaction

class CartSKUSerializer(serializers.ModelSerializer):
    """订单中的商品序列化器"""
    count = serializers.IntegerField(label='商品的购买数量')

    class Meta:
        model = SKU
        fields = ['id', 'name', 'default_image_url', 'price', 'count']

class OrderSettlementSerializer(serializers.Serializer):
    """订单序列化器"""
    skus = CartSKUSerializer(many=True)
    freight = serializers.DecimalField(label='运费', max_digits=10, decimal_places=2)

class CommitOrderSerializer(serializers.ModelSerializer):
    """保存订单的序列化器"""

    class Meta:
        model = OrderInfo
        fields = ['address', 'pay_method', 'order_id']
        read_only_fields = ['order_id'] # 只序列化输出 不做反序列化
        extra_kwargs = {
            'address': {'write_only': True},
            'pay_method': {'write_only': True},
        }

    def create(self, validated_data):
        """同时操作四张表 订单基本信息表 sku表 spu表 订单中商品表 """
        """保存订单"""
        # 获取当前保存订单时需要的信息

        # 订单编号：当前时间 + 000000001
        user = self.context['request'].user
        order_id = datetime.now().strftime('%Y%m%d%H%M%S') + '%09d' % user.id
        # 获取前端传入的收获地址
        address = validated_data.get('address')
        # 获取前端传入的支付方式
        pay_method = validated_data.get('pay_method')
        # 订单状态
        status = (OrderInfo.ORDER_STATUS_ENUM['UNPAID']
                  if pay_method == OrderInfo.PAY_METHODS_ENUM['ALIPAY']
                  else OrderInfo.ORDER_STATUS_ENUM['UNSEND'])


        # 手动开启事务
        with transaction.atomic():
            # 创建事务的保存点
            save_point = transaction.savepoint()
            try:
                # 保存订单基本信息OrderInfo
                orderInfo = OrderInfo.objects.create(
                    order_id=order_id,
                    user=user,
                    address=address,
                    total_count=0,
                    total_amount=Decimal('0.00'),
                    freight=Decimal('10.00'),
                    pay_method=pay_method,
                    status=status
                )
                # 从redis读取购物车中被勾选的商品信息
                redis_conn = get_redis_connection('cart')
                cart_dict_redis = redis_conn.hgetall('cart_%d' % user.id)
                selected_ids = redis_conn.smembers('selected_%d' % user.id)

                # 遍历购物车中被勾选的商品信息
                for sku_id_bytes in selected_ids:
                    # 让用户对同一个商品 有无限次下单机会 直到库存为0
                    while True:

                        # 获取sku对象
                        sku = SKU.objects.get(id=sku_id_bytes)
                        # 获取当前商品的购买数量
                        buy_count = int(cart_dict_redis[sku_id_bytes])
                        # 把当前sku模型中的库存和销量都分别获取出来
                        origin_sales = sku.sales# 表示当前商品的销量
                        origin_stock = sku.stock# 表示当前商品的库存
                        # 判断库存
                        if buy_count > origin_stock:
                            raise serializers.ValidationError('库存不足')
                        # 减少库存 增加销量 sku
                        # 计算新的库存和销量
                        new_sales = origin_sales + buy_count
                        new_stock = origin_stock - buy_count


                        # sku.sales = new_sales# 修改sku模型的销量
                        # sku.stock = new_stock# 修改sku模型的库存
                        # sku.save()
                        # 采用乐观锁 原有的库存和id 判断 update更新时 返回更新数据的条数
                        result = SKU.objects.filter(stock=origin_stock, id=sku_id_bytes).update(stock=new_stock, sales=new_sales)
                        if result == 0: # 如果没有修改成功 则有抢夺
                            continue

                        #修改SPU销量
                        spu = sku.goods
                        spu.sales  = spu.sales + buy_count
                        spu.save()
                        # 保存订单商品信息 OrderGoods
                        OrderGoods.objects.create(
                            order=orderInfo,
                            sku=sku,
                            count=buy_count,
                            price=sku.price,
                        )
                        # 累加计算总数量和总价
                        orderInfo.total_count += buy_count
                        orderInfo.total_amount += (sku.price * buy_count)

                        break # 当前这个商品下单成功 跳出死循环, 进行对下一个商品下单

                # 最后加入邮费和保存订单信息
                orderInfo.total_amount += orderInfo.freight
                orderInfo.save()
            except Exception as e:
                # 证明中间四张表有异常 进行暴力回滚
                transaction.savepoint_rollback(save_point)
                raise serializers.ValidationError('库存不足')
            else:
                # 如果中间没有出现问题 手动提交事务
                transaction.savepoint_commit(save_point)
        # 清楚购物车中已结算的商品
        pl = redis_conn.pipeline()
        pl.hdel('cart_%d' % user.id, *selected_ids)
        pl.srem('selected_%d' % user.id, *selected_ids)
        pl.execute()

        # 返回订单模型对象
        return orderInfo


